package cool.taomu.toolkit.quartz

import java.lang.reflect.Method
import org.quartz.Job
import org.quartz.JobExecutionContext
import org.slf4j.LoggerFactory

class QuartzInvokerJob implements Job {
    val static LOG = LoggerFactory.getLogger(QuartzMethodInterceptor);
    public static val String TARGET_OBJECT = "targetObject";
    public static val String METHOD_NAME = "methodName";

    override void execute(JobExecutionContext context) {
        var Thread current = Thread.currentThread();
        LOG.debug("线程名：" + current.getName() + "，是否为守护线程：" + current.isDaemon());
        var dataMap = context.getMergedJobDataMap();
        var Object target = dataMap.get(TARGET_OBJECT);
        var String methodName = dataMap.getString(METHOD_NAME);
        LOG.info("target:{},method name:{}", target, methodName)
        try {
            var Method method = target.getClass().getMethod(methodName);
            method.invoke(target);
        } catch (Exception e) {
            throw new RuntimeException("执行定时任务失败", e);
        }
    }
}
